package com.sspku.weinotes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sspku.weinotes.entity.Note;
import com.sspku.weinotes.entity.NoteBook;
import com.sspku.weinotes.exception.IncorrectParameterException;
import com.sspku.weinotes.exception.NoSuchIdException;
import com.sspku.weinotes.mapper.NoteBookMapper;
import com.sspku.weinotes.service.NoteService;
import com.sspku.weinotes.mapper.NoteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
* @author user
* @description 针对表【note】的数据库操作Service实现
*
*/
@Service
public class NoteServiceImpl extends ServiceImpl<NoteMapper, Note>
    implements NoteService{
    private static final Logger logger = LoggerFactory.getLogger(NoteBookServiceImpl.class);
    @Autowired
    private NoteMapper noteMapper;
    @Autowired
    private NoteBookMapper noteBookMapper;

    @Override
    public List<Note> getAllNotes(int noteBookId) {
        try {
            LambdaQueryWrapper<Note> lqw = new LambdaQueryWrapper<Note>();
            lqw.eq(Note::getBid, noteBookId);
            return noteMapper.selectList(lqw);
        } catch (Exception e) {
            logger.info("get all notes error ", e);
            return null;
        }
    }

    @Override
    public Note getNoteByNoteId(int noteId) {
        try {
            return noteMapper.selectById(noteId);
        } catch (Exception e) {
            logger.info("get note error ", e);
            return null;
        }
    }

    @Override
    public boolean addNote(int noteBookId, String title, String content) throws IncorrectParameterException, NoSuchIdException {
        if (StringUtils.isBlank(title)) {
            logger.info("add note parameter exception");
            throw new IncorrectParameterException("标题不能为空");
        }

        Note note = new Note();
        //ID组成: 笔记本ID+笔记ID
        note.setTitle(title);
        note.setContent(content);
        note.setGmtCreate(new Date());
        note.setGmtModified(new Date());
        note.setTrash(0);
        note.setBid(noteBookId);
        try {
            return noteMapper.insert(note)>0;
        } catch (Exception e) {
            logger.info("save note error ", e);
            return false;
        }
    }

    @Override
    public boolean modifyNote(int note_id, String title, String content) throws IncorrectParameterException, NoSuchIdException {
        if (StringUtils.isBlank(title)) {
            logger.info("add note parameter exception");
            throw new IncorrectParameterException("标题不能为空");
        }
        try {
            Note note = noteMapper.selectById(note_id);
            if (note == null) throw  new NoSuchIdException("笔记ID不存在");
            note.setTitle(title);
            note.setContent(content);
            note.setGmtModified(new Date());
            return noteMapper.updateById(note)>0;
        } catch (Exception e) {
            logger.info("modify note error ", e);
            throw new IncorrectParameterException("参数不正确");
        }
    }

    @Override
    public boolean deleteByNoteId(int id) {
        try {
            return noteMapper.deleteById(id)>0;
        } catch (Exception e) {
            logger.info("delete note error ", e);
            return false;
        }
    }
    @Override
    public boolean deleteByNoteBookId(int noteBookId) {
        try {
            QueryWrapper<Note>queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("bid",noteBookId);
            noteMapper.delete(queryWrapper);
            return true;
        } catch (Exception e) {
            logger.info("batch delete notes error ", e);
            return false;
        }
    }

    @Override
    public Note getNote(Integer id) {
        return noteMapper.selectById(id);
    }
@Override
   public List<Note> getAlive(Integer id){
        QueryWrapper<Note>queryWrapper=new QueryWrapper<>();

        queryWrapper.eq("bid",id);
       queryWrapper.eq("trash",0);
       return noteMapper.selectList(queryWrapper);
   }
   @Override
  public List<Note> getTrash(Integer id){
       QueryWrapper<Note>queryWrapper=new QueryWrapper<>();

       queryWrapper.eq("bid",id);
       queryWrapper.eq("trash",1);
       return noteMapper.selectList(queryWrapper);
   }
   @Override
    public int patchTrash(Integer id) throws NoSuchIdException, IncorrectParameterException {
       Note note=noteMapper.selectById(id);
       if(note==null) throw new NoSuchIdException("该笔记不存在");
       if(note.getTrash()==1) throw new IncorrectParameterException("该笔记已经在垃圾箱了");
       note.setTrash(1);
       note.setGmtModified(new Date());
       return noteMapper.updateById(note);
   }
   @Override
   public int patchAlive(Integer id) throws NoSuchIdException, IncorrectParameterException {
       Note note=noteMapper.selectById(id);
       if(note==null) throw new NoSuchIdException("该笔记不存在");
       if(note.getTrash()==0) throw new IncorrectParameterException("该笔记不在垃圾箱");
       note.setTrash(0);
       note.setGmtModified(new Date());
       return noteMapper.updateById(note);
   }
}




